//
//  ExpertDetailsMsg.m
//
//  Created by 啊  on 16/5/10
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "ExpertDetailsMsg.h"
#import "ExpertDetailsExpertAppraise.h"
#import "ExpertDetailsArticle.h"
#import "ExpertDetailsSimilarityExpert.h"
#import "ExpertDetailsExpertProperty.h"


NSString *const kExpertDetailsMsgEHeadUrl = @"eHeadUrl";
NSString *const kExpertDetailsMsgExpertAppraise = @"ExpertAppraise";
NSString *const kExpertDetailsMsgEName = @"eName";
NSString *const kExpertDetailsMsgECoverUrl = @"eCoverUrl";
NSString *const kExpertDetailsMsgECompany = @"eCompany";
NSString *const kExpertDetailsMsgEViewNum = @"eViewNum";
NSString *const kExpertDetailsMsgEMaxAge = @"eMaxAge";
NSString *const kExpertDetailsMsgEMinAge = @"eMinAge";
NSString *const kExpertDetailsMsgArticle = @"Article";
NSString *const kExpertDetailsMsgSimilarityExpert = @"similarityExpert";
NSString *const kExpertDetailsMsgEDiscussion = @"eDiscussion";
NSString *const kExpertDetailsMsgExpertProperty = @"ExpertProperty";
NSString *const kExpertDetailsMsgEInfo = @"eInfo";
NSString *const kExpertDetailsMsgSubscription = @"subscription";
NSString *const kExpertDetailsMsgEAppellation = @"eAppellation";
NSString *const kExpertDetailsMsgEConsultancyNum = @"eConsultancyNum";
NSString *const kExpertDetailsMsgEId = @"eId";
NSString *const kExpertDetailsMsgEFavoriteNum = @"eFavoriteNum";
NSString *const kExpertDetailsMsgEBirthYear = @"eBirthYear";


@interface ExpertDetailsMsg ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ExpertDetailsMsg

@synthesize eHeadUrl = _eHeadUrl;
@synthesize expertAppraise = _expertAppraise;
@synthesize eName = _eName;
@synthesize eCoverUrl = _eCoverUrl;
@synthesize eCompany = _eCompany;
@synthesize eViewNum = _eViewNum;
@synthesize eMaxAge = _eMaxAge;
@synthesize eMinAge = _eMinAge;
@synthesize article = _article;
@synthesize similarityExpert = _similarityExpert;
@synthesize eDiscussion = _eDiscussion;
@synthesize expertProperty = _expertProperty;
@synthesize eInfo = _eInfo;
@synthesize subscription = _subscription;
@synthesize eAppellation = _eAppellation;
@synthesize eConsultancyNum = _eConsultancyNum;
@synthesize eId = _eId;
@synthesize eFavoriteNum = _eFavoriteNum;
@synthesize eBirthYear = _eBirthYear;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.eHeadUrl = [self objectOrNilForKey:kExpertDetailsMsgEHeadUrl fromDictionary:dict];
    NSObject *receivedExpertDetailsExpertAppraise = [dict objectForKey:kExpertDetailsMsgExpertAppraise];
    NSMutableArray *parsedExpertDetailsExpertAppraise = [NSMutableArray array];
    if ([receivedExpertDetailsExpertAppraise isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedExpertDetailsExpertAppraise) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedExpertDetailsExpertAppraise addObject:[ExpertDetailsExpertAppraise modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedExpertDetailsExpertAppraise isKindOfClass:[NSDictionary class]]) {
       [parsedExpertDetailsExpertAppraise addObject:[ExpertDetailsExpertAppraise modelObjectWithDictionary:(NSDictionary *)receivedExpertDetailsExpertAppraise]];
    }

    self.expertAppraise = [NSArray arrayWithArray:parsedExpertDetailsExpertAppraise];
            self.eName = [self objectOrNilForKey:kExpertDetailsMsgEName fromDictionary:dict];
            self.eCoverUrl = [self objectOrNilForKey:kExpertDetailsMsgECoverUrl fromDictionary:dict];
            self.eCompany = [self objectOrNilForKey:kExpertDetailsMsgECompany fromDictionary:dict];
            self.eViewNum = [[self objectOrNilForKey:kExpertDetailsMsgEViewNum fromDictionary:dict] doubleValue];
            self.eMaxAge = [[self objectOrNilForKey:kExpertDetailsMsgEMaxAge fromDictionary:dict] doubleValue];
            self.eMinAge = [[self objectOrNilForKey:kExpertDetailsMsgEMinAge fromDictionary:dict] doubleValue];
    NSObject *receivedExpertDetailsArticle = [dict objectForKey:kExpertDetailsMsgArticle];
    NSMutableArray *parsedExpertDetailsArticle = [NSMutableArray array];
    if ([receivedExpertDetailsArticle isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedExpertDetailsArticle) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedExpertDetailsArticle addObject:[ExpertDetailsArticle modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedExpertDetailsArticle isKindOfClass:[NSDictionary class]]) {
       [parsedExpertDetailsArticle addObject:[ExpertDetailsArticle modelObjectWithDictionary:(NSDictionary *)receivedExpertDetailsArticle]];
    }

    self.article = [NSArray arrayWithArray:parsedExpertDetailsArticle];
    NSObject *receivedExpertDetailsSimilarityExpert = [dict objectForKey:kExpertDetailsMsgSimilarityExpert];
    NSMutableArray *parsedExpertDetailsSimilarityExpert = [NSMutableArray array];
    if ([receivedExpertDetailsSimilarityExpert isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedExpertDetailsSimilarityExpert) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedExpertDetailsSimilarityExpert addObject:[ExpertDetailsSimilarityExpert modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedExpertDetailsSimilarityExpert isKindOfClass:[NSDictionary class]]) {
       [parsedExpertDetailsSimilarityExpert addObject:[ExpertDetailsSimilarityExpert modelObjectWithDictionary:(NSDictionary *)receivedExpertDetailsSimilarityExpert]];
    }

    self.similarityExpert = [NSArray arrayWithArray:parsedExpertDetailsSimilarityExpert];
            self.eDiscussion = [self objectOrNilForKey:kExpertDetailsMsgEDiscussion fromDictionary:dict];
    NSObject *receivedExpertDetailsExpertProperty = [dict objectForKey:kExpertDetailsMsgExpertProperty];
    NSMutableArray *parsedExpertDetailsExpertProperty = [NSMutableArray array];
    if ([receivedExpertDetailsExpertProperty isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedExpertDetailsExpertProperty) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedExpertDetailsExpertProperty addObject:[ExpertDetailsExpertProperty modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedExpertDetailsExpertProperty isKindOfClass:[NSDictionary class]]) {
       [parsedExpertDetailsExpertProperty addObject:[ExpertDetailsExpertProperty modelObjectWithDictionary:(NSDictionary *)receivedExpertDetailsExpertProperty]];
    }

    self.expertProperty = [NSArray arrayWithArray:parsedExpertDetailsExpertProperty];
            self.eInfo = [self objectOrNilForKey:kExpertDetailsMsgEInfo fromDictionary:dict];
            self.subscription = [[self objectOrNilForKey:kExpertDetailsMsgSubscription fromDictionary:dict] doubleValue];
            self.eAppellation = [self objectOrNilForKey:kExpertDetailsMsgEAppellation fromDictionary:dict];
            self.eConsultancyNum = [[self objectOrNilForKey:kExpertDetailsMsgEConsultancyNum fromDictionary:dict] doubleValue];
            self.eId = [[self objectOrNilForKey:kExpertDetailsMsgEId fromDictionary:dict] doubleValue];
            self.eFavoriteNum = [[self objectOrNilForKey:kExpertDetailsMsgEFavoriteNum fromDictionary:dict] doubleValue];
            self.eBirthYear = [self objectOrNilForKey:kExpertDetailsMsgEBirthYear fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.eHeadUrl forKey:kExpertDetailsMsgEHeadUrl];
    NSMutableArray *tempArrayForExpertAppraise = [NSMutableArray array];
    for (NSObject *subArrayObject in self.expertAppraise) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForExpertAppraise addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForExpertAppraise addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForExpertAppraise] forKey:kExpertDetailsMsgExpertAppraise];
    [mutableDict setValue:self.eName forKey:kExpertDetailsMsgEName];
    [mutableDict setValue:self.eCoverUrl forKey:kExpertDetailsMsgECoverUrl];
    [mutableDict setValue:self.eCompany forKey:kExpertDetailsMsgECompany];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eViewNum] forKey:kExpertDetailsMsgEViewNum];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eMaxAge] forKey:kExpertDetailsMsgEMaxAge];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eMinAge] forKey:kExpertDetailsMsgEMinAge];
    NSMutableArray *tempArrayForArticle = [NSMutableArray array];
    for (NSObject *subArrayObject in self.article) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForArticle addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForArticle addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForArticle] forKey:kExpertDetailsMsgArticle];
    NSMutableArray *tempArrayForSimilarityExpert = [NSMutableArray array];
    for (NSObject *subArrayObject in self.similarityExpert) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForSimilarityExpert addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForSimilarityExpert addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForSimilarityExpert] forKey:kExpertDetailsMsgSimilarityExpert];
    [mutableDict setValue:self.eDiscussion forKey:kExpertDetailsMsgEDiscussion];
    NSMutableArray *tempArrayForExpertProperty = [NSMutableArray array];
    for (NSObject *subArrayObject in self.expertProperty) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForExpertProperty addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForExpertProperty addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForExpertProperty] forKey:kExpertDetailsMsgExpertProperty];
    [mutableDict setValue:self.eInfo forKey:kExpertDetailsMsgEInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.subscription] forKey:kExpertDetailsMsgSubscription];
    [mutableDict setValue:self.eAppellation forKey:kExpertDetailsMsgEAppellation];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eConsultancyNum] forKey:kExpertDetailsMsgEConsultancyNum];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eId] forKey:kExpertDetailsMsgEId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eFavoriteNum] forKey:kExpertDetailsMsgEFavoriteNum];
    [mutableDict setValue:self.eBirthYear forKey:kExpertDetailsMsgEBirthYear];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.eHeadUrl = [aDecoder decodeObjectForKey:kExpertDetailsMsgEHeadUrl];
    self.expertAppraise = [aDecoder decodeObjectForKey:kExpertDetailsMsgExpertAppraise];
    self.eName = [aDecoder decodeObjectForKey:kExpertDetailsMsgEName];
    self.eCoverUrl = [aDecoder decodeObjectForKey:kExpertDetailsMsgECoverUrl];
    self.eCompany = [aDecoder decodeObjectForKey:kExpertDetailsMsgECompany];
    self.eViewNum = [aDecoder decodeDoubleForKey:kExpertDetailsMsgEViewNum];
    self.eMaxAge = [aDecoder decodeDoubleForKey:kExpertDetailsMsgEMaxAge];
    self.eMinAge = [aDecoder decodeDoubleForKey:kExpertDetailsMsgEMinAge];
    self.article = [aDecoder decodeObjectForKey:kExpertDetailsMsgArticle];
    self.similarityExpert = [aDecoder decodeObjectForKey:kExpertDetailsMsgSimilarityExpert];
    self.eDiscussion = [aDecoder decodeObjectForKey:kExpertDetailsMsgEDiscussion];
    self.expertProperty = [aDecoder decodeObjectForKey:kExpertDetailsMsgExpertProperty];
    self.eInfo = [aDecoder decodeObjectForKey:kExpertDetailsMsgEInfo];
    self.subscription = [aDecoder decodeDoubleForKey:kExpertDetailsMsgSubscription];
    self.eAppellation = [aDecoder decodeObjectForKey:kExpertDetailsMsgEAppellation];
    self.eConsultancyNum = [aDecoder decodeDoubleForKey:kExpertDetailsMsgEConsultancyNum];
    self.eId = [aDecoder decodeDoubleForKey:kExpertDetailsMsgEId];
    self.eFavoriteNum = [aDecoder decodeDoubleForKey:kExpertDetailsMsgEFavoriteNum];
    self.eBirthYear = [aDecoder decodeObjectForKey:kExpertDetailsMsgEBirthYear];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_eHeadUrl forKey:kExpertDetailsMsgEHeadUrl];
    [aCoder encodeObject:_expertAppraise forKey:kExpertDetailsMsgExpertAppraise];
    [aCoder encodeObject:_eName forKey:kExpertDetailsMsgEName];
    [aCoder encodeObject:_eCoverUrl forKey:kExpertDetailsMsgECoverUrl];
    [aCoder encodeObject:_eCompany forKey:kExpertDetailsMsgECompany];
    [aCoder encodeDouble:_eViewNum forKey:kExpertDetailsMsgEViewNum];
    [aCoder encodeDouble:_eMaxAge forKey:kExpertDetailsMsgEMaxAge];
    [aCoder encodeDouble:_eMinAge forKey:kExpertDetailsMsgEMinAge];
    [aCoder encodeObject:_article forKey:kExpertDetailsMsgArticle];
    [aCoder encodeObject:_similarityExpert forKey:kExpertDetailsMsgSimilarityExpert];
    [aCoder encodeObject:_eDiscussion forKey:kExpertDetailsMsgEDiscussion];
    [aCoder encodeObject:_expertProperty forKey:kExpertDetailsMsgExpertProperty];
    [aCoder encodeObject:_eInfo forKey:kExpertDetailsMsgEInfo];
    [aCoder encodeDouble:_subscription forKey:kExpertDetailsMsgSubscription];
    [aCoder encodeObject:_eAppellation forKey:kExpertDetailsMsgEAppellation];
    [aCoder encodeDouble:_eConsultancyNum forKey:kExpertDetailsMsgEConsultancyNum];
    [aCoder encodeDouble:_eId forKey:kExpertDetailsMsgEId];
    [aCoder encodeDouble:_eFavoriteNum forKey:kExpertDetailsMsgEFavoriteNum];
    [aCoder encodeObject:_eBirthYear forKey:kExpertDetailsMsgEBirthYear];
}

- (id)copyWithZone:(NSZone *)zone
{
    ExpertDetailsMsg *copy = [[ExpertDetailsMsg alloc] init];
    
    if (copy) {

        copy.eHeadUrl = [self.eHeadUrl copyWithZone:zone];
        copy.expertAppraise = [self.expertAppraise copyWithZone:zone];
        copy.eName = [self.eName copyWithZone:zone];
        copy.eCoverUrl = [self.eCoverUrl copyWithZone:zone];
        copy.eCompany = [self.eCompany copyWithZone:zone];
        copy.eViewNum = self.eViewNum;
        copy.eMaxAge = self.eMaxAge;
        copy.eMinAge = self.eMinAge;
        copy.article = [self.article copyWithZone:zone];
        copy.similarityExpert = [self.similarityExpert copyWithZone:zone];
        copy.eDiscussion = [self.eDiscussion copyWithZone:zone];
        copy.expertProperty = [self.expertProperty copyWithZone:zone];
        copy.eInfo = [self.eInfo copyWithZone:zone];
        copy.subscription = self.subscription;
        copy.eAppellation = [self.eAppellation copyWithZone:zone];
        copy.eConsultancyNum = self.eConsultancyNum;
        copy.eId = self.eId;
        copy.eFavoriteNum = self.eFavoriteNum;
        copy.eBirthYear = [self.eBirthYear copyWithZone:zone];
    }
    
    return copy;
}


@end
